﻿LOAIBPBK = function () {

}
LOAIBPBK.columns = new Array("Mã Loại Bpbk", "Tên Loại Bpbk");
LOAIBPBK.fieldText = new Array("MaLoaiBpbk", "TenLoaiBpbk");
LOAIBPBK.MaLoaiBpbk = -1;
var numberPage = 1;
LOAIBPBK.isInsert = 0;
LOAIBPBK.arrays = new Array();
LOAIBPBK.Init = function () {
    LOAIBPBK.arrays = new Array();
    var obj;
    obj = new CheckField("TenLoaiBpbk", 1, "ErrTenLoaiBpbk");
    LOAIBPBK.Add(obj);
}

LOAIBPBK.Add = function (obj) {
    if (LOAIBPBK.arrays.contains(obj) == false) {
        LOAIBPBK.arrays.push(obj);
    }
}

LOAIBPBK.Insert = function () {
    var serviceUrl = Constants.service + "insertTblLoaiBpbks";
    if (Constants.OnSubmit(LOAIBPBK.arrays) == false) {
        return false;
    }
    var data = {
        MaLoaiBpbk: $("#MaLoaiBpbk").val(),
        TenLoaiBpbk: $("#TenLoaiBpbk").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            LOAIBPBK.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

LOAIBPBK.Delete = function (id) {    
    var data = {
        MaLoaiBpbk: id
    };
    var json = JSON.stringify({ item: data });
    a = confirm(Constants.messDelete);
    if (a == false) {
        return;
    }
    var serviceUrl = Constants.service + "deleteTblLoaiBpbks";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (data) {
            LOAIBPBK.GetData();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

LOAIBPBK.UpdateItem = function () {
    var serviceUrl = Constants.service + "updateTblLoaiBpbks";
    if (Constants.OnSubmit(LOAIBPBK.arrays) == false) {
        return false;
    }
    var data = {
        MaLoaiBpbk: $("#MaLoaiBpbk").val(),
        TenLoaiBpbk: $("#TenLoaiBpbk").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            LOAIBPBK.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

LOAIBPBK.CreateTable = function (r, nbpage, data) {
    var myTable = LOAIBPBK.TitleColumns(LOAIBPBK.columns);
    myTable += LOAIBPBK.DataColumnsOftr(data, r, nbpage);
    myTable += "</table>";
    $("#div_list").children().remove();
    $("#div_list").append(myTable);
    $("#div_content").show();
}

LOAIBPBK.GetData = function () {
    var serviceUrl = Constants.service + "getAllTblLoaiBpbks";
    var data = { url: window.location.host };
    var json = JSON.stringify({ item: data });
    LOAIBPBK.isInsert = 0;
    LOAIBPBK.MaLoaiBpbk = -1;
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var results = msg.getAllTblLoaiBpbksResult;
            var dataTables = results;
            LOAIBPBK.CreateTable(Constants.rows, numberPage, dataTables);
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

LOAIBPBK.TitleColumns = function (_columns) {
    var myColumns = "<table class=\"rounded-corner\">";
    myColumns += "<thead><tr>";
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"rounded\" scope=\"col\">" + _columns[i] + "</th>";
    }
    myColumns += "<th class=\"rounded\" scope=\"col\">Edit</th>";
    myColumns += "<th class=\"rounded-q4\" scope=\"col\">Delete</th>";
    myColumns += "</tr></thead>";
    var n = _columns.length + 1;
    myColumns += "<tfoot><tr><td class=\"rounded-foot-left\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"rounded-foot-right\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}

LOAIBPBK.DataColumnsOftr = function (data, rs, _numberPage) {
    var myDataColumns = "<tbody>";
    var n = data.length;
    var j = (_numberPage - 1) * rs;
    var r = rs;        
    for (var i = j; i < r && i < n; i++) {
        var id = data[i].MaLoaiBpbk;
        myDataColumns += "<tr onclick='return LOAIBPBK.OnClick(" + id + ")'" + " id=\"" + id + "\">";        
        myDataColumns += "<td>" + data[i].MaLoaiBpbk + "</td>"
        myDataColumns += "<td>" + data[i].TenLoaiBpbk + "</td>"
        myDataColumns += "<td><img onclick='return LOAIBPBK.EditItem("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/user_edit.png\"></td>";
        myDataColumns += "<td><a class=\"ask\" ><img onclick='return LOAIBPBK.Delete(" + id + ")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/trash.png\"></a></td>";
    }
    myDataColumns += "</tbody>";
    return myDataColumns;
}

LOAIBPBK.OnClick = function (id) {
    LOAIBPBK.MaLoaiBpbk = id;
    $(".active").removeClass();
    $("#" + id).addClass("active");
    return false;
}

LOAIBPBK.OnClickEdit = function () {
    if (LOAIBPBK.MaLoaiBpbk == -1) {
        alert(Constants.selectEdit);
    }
    else
        LOAIBPBK.EditItem(LOAIBPBK.MaLoaiBpbk);
}

LOAIBPBK.OnClickDelete = function () {
    if (LOAIBPBK.MaLoaiBpbk == -1) {
        alert(Constants.selectDelete);
    }
    else
        LOAIBPBK.Delete(LOAIBPBK.MaLoaiBpbk);
}

LOAIBPBK.EditItem = function (obj_id) {    
    var temp_item = "Modules/lbpbk/Edit";   
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Sửa" });
            LOAIBPBK.GetItem(obj_id);                       
        }
    });
}

LOAIBPBK.GetItem = function (id) {
    var serviceUrl = Constants.service + "getItemByIdTblLoaiBpbk";
    var data = { MaLoaiBpbk: id };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getItemByIdTblLoaiBpbkResult;
            if (result != null && result != undefined) {
                $("#MaLoaiBpbk").val(result.MaLoaiBpbk);
                $("#TenLoaiBpbk").val(result.TenLoaiBpbk);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

LOAIBPBK.Clear = function () {
    Constants.clearAll(LOAIBPBK.fieldText);
}

LOAIBPBK.AddNewItem = function () {
    LOAIBPBK.isInsert = 1;
    var temp_item = "Modules/lbpbk/AddNew";  
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Thêm Mới" });
            LOAIBPBK.getMa();
        }
    });
}

LOAIBPBK.getMa = function () {
    var serviceUrl = Constants.service + "getMaTblLoaiBpbk";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        //data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getMaTblLoaiBpbkResult;
            if (result != undefined && result != null) {
                $("#MaLoaiBpbk").val(result);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

LOAIBPBK.Enter = function () {
    $("#MaLoaiBpbk").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("#TenLoaiBpbk").focus();
        }
    });
}
LOAIBPBK.Search = function () {
    var serviceUrl = Constants.service + "getFindTblLoaiBpbks";
    var data = {
        value: $("#search").val()
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl,
        data: json,
        contentType: "application/json",
        dataType: "json",
        cache: false,
        success: function (msg) {
            var results = msg.getFindTblLoaiBpbksResult;
            if (results != null && results.length > 0) {
                LOAIBPBK.CreateTable(Constants.rows, numberPage, results);
            }
            else {
                $("#div_list").children().remove();
                $("#div_list").html(Constants.find);
            }
        }
    });
}

$(document).ready(function () {
    LOAIBPBK.GetData();
    LOAIBPBK.Init();
    $("#search").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            LOAIBPBK.Search();
        }
    });
    $("#search_on").click(function (event) {
        event.preventDefault();
        LOAIBPBK.Search();
    });
});